package org.javaboy.demo.converter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class MyDateConverter implements Converter<String, Date> {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 这个参数就是前端传来的日期字符串
     *
     * @param source the source object to convert, which must be an instance of {@code S} (never {@code null})
     * @return 返回值就是日期类型
     */
    @Override
    public Date convert(String source) {
        try {
            return sdf.parse(source);
        } catch (ParseException e) {
//            throw new RuntimeException(e);
        }
        return null;
    }
}
